import React from 'react';
import { Navigate } from 'react-router-dom';
import AuthService from '../services/AuthService';

const AdminRoute = ({ children }) => {
  const user = AuthService.getCurrentUser();
  console.log('AdminRoute - 当前用户:', user);
  
  // 同时检查两种可能的数据结构
  if (!user || !AuthService.isAuthenticated()) {
    console.log('AdminRoute - 用户未认证');
    return <Navigate to="/login" replace />;
  }
  
  // 检查role是直接在user对象上还是在user.user中
  if ((user.role && user.role === 'ROLE_ADMIN') || 
      (user.user && user.user.role === 'ROLE_ADMIN')) {
    console.log('AdminRoute - 验证为管理员');
    return children;
  } else {
    console.log('AdminRoute - 不是管理员');
    return <Navigate to="/login" replace />;
  }
};

export default AdminRoute; 